package arithmetic.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 生产消费模式
 * @author jiangfeng on 2022/4/6
 */
public class ProducerAndConsumer {

    public static Object p  = new Object();
    public static Object c  = new Object();
    public static AtomicInteger pSize = new AtomicInteger() ;
    public static AtomicInteger cSize = new AtomicInteger() ;

    public static void main(String[] args) {

        Thread pt1 = new Thread(() -> {
            while (true) {
                System.out.println("生产一个1");
                pSize.incrementAndGet();
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });

        Thread pt2 = new Thread(() -> {
            while (true) {
                System.out.println("生产一个2");
                pSize.incrementAndGet();
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });

        Thread ct = new Thread(() -> {
            while (true) {

                System.out.println("消费一个1");
                cSize.incrementAndGet();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread ct2 = new Thread(() -> {
            while (true) {

                System.out.println("消费一个2");
                cSize.incrementAndGet();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });





    }

}
